<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel <jducastel@jducastel.fr>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* internationalization functionalities service
* @author J.Ducastel <jducastel@jducastel.fr>
* @version 
*/
class i18n extends wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	* /
	function service_version() {
		$this->__construct();
	}/**/
	
	/**
	* "real" constructor
	* @access
	* /
	function __construct() {
		parent::__construct();
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* parses http accept language string
	* returns an array sorted by coefficient desc
	* @param $accept optional, default to $_SERVER['HTTP_ACCEPT_LANGUAGE']
	* @access public
	* @return array [coef.n]=>lang
	*/
	function parseHttpAcceptLanguage($accept=null) {
		// getting http instruction
		if (!is_string($accept))
			$accept=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
		// exploding accepted languages 
		$parts=explode(',',$accept);
		// creating output list
		$accepted=array(); $n=99;
		foreach ($parts as $part) {
			// parsing language preference instructions
			// 2_digit_code[-longer_code][;q=coefficient]
			ereg('([a-zA-Z]{1,2}(-[a-zA-Z0-9]+)?)(;q=([0-9\.]+))?',$part,$found);
			//echo wakapi::vardump($found);
			// 2 digit lang code
			$lang=$found[1];
			// coefficient
			$coef=sprintf('%3.1f',$found[4]?$found[4]:'1');
			// for sorting by coefficient
			
			$key=$coef.'.'.$n;
			$n--;
			// adding 
			$accepted[$key]=$lang;
		}
		// sorting the list by coefficient desc
		krsort($accepted);
		return $accepted;
	}/**/
	
	/**
	* returns best language code from user setting and proposed list
	* user setting is defaulted to browser setting
	* @param array|string $userLang defaulted to browser setting
	* @access public
	* @return string lang code
	*/
	function getBestLang($userLang=null, $proposed=null) {
		// get user lang
		if (is_null($userLang)) {
			$userLang=$this->parseHttpAcceptLanguage();
		}
		// get proposed languages
		if (is_null($proposed)) {
			$proposed=$this->cfg['proposed_languages'];
		}
		// cycling through user languages, returning first match
		foreach ($userLang as $lang) {
			if (in_array($lang,$proposed))
				return $lang;
		}
		// user languages are not proposed, defaulting to first
		return $proposed[0];
	}/**/
	
	/**
	* get language defined for use
	* @param
	* @access public
	* @return string lang code
	*/
	function getLang() {
		return $this->getBestLang();
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	var $cfg=array(
		'proposed_languages'=>array('en')
	);
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
